﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using PMC.Plugins;

namespace PMC.Handlers
{
    public class EPluginHandler
    {
        /// <summary>
        /// Singleton
        /// </summary>
        private static EPluginHandler _instance = null;
        public static EPluginHandler Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new EPluginHandler();
                return _instance;
            }
        }

        #region Properties
        public List<Plugin> Plugins { get; private set; }
        public List<Plugin> UriCollectorPlugins
        {
            get
            {
                List<Plugin> returnList = new List<Plugin>();
                foreach (Plugin p in Plugins)
                {
                    if (p.Plug == typeof(UriCollectorPlugin))
                        returnList.Add(p);
                }
                return returnList;
            }
        }
        public List<Plugin> StatePlugins
        {
            get
            {
                List<Plugin> returnList = new List<Plugin>();
                foreach (Plugin p in Plugins)
                {
                    if (p.Plug == typeof(StatePlugin))
                        returnList.Add(p);
                }
                return returnList;
            }
        }
        #endregion

        private EPluginHandler()
        {
            FindPlugins();
        }

        private void FindPlugins()
        {
            String pluginFolder = Directory.GetCurrentDirectory() + Properties.Settings.Default.PluginFolder;
            Plugins = new List<Plugins.Plugin>();

            //Checks wether pluginfolder exists
            if (!Directory.Exists(pluginFolder))
                throw new ArgumentException(pluginFolder + " not found!");

            //Loops through folder
            foreach (String f in Directory.GetFiles(pluginFolder, "*.DLL"))
            {
                //Foreach DLL file get Plugins available in library
                foreach (Plugin p in Plugin.FromFile(f))
                    Plugins.Add(p); //Add all plugins to the list
            }
        }

        //Lot of copy pastin' going on here!
        public static Plugin CaptchaState { set { if (value.Plug == typeof(StatePlugin)) CaptchaState = value; } get { return CaptchaState; } }
        public static Plugin NormalState { set { if (value.Plug == typeof(StatePlugin)) NormalState = value; } get { return NormalState; } }
        public static Plugin LoginState { set { if (value.Plug == typeof(StatePlugin)) LoginState = value; } get { return LoginState; } }
        public static Plugin SkipMessageState { set { if (value.Plug == typeof(StatePlugin)) SkipMessageState = value; } get { return SkipMessageState; } }
        public static Plugin NoInternetConnectionState { set { if (value.Plug == typeof(StatePlugin)) NoInternetConnectionState = value; } get { return NoInternetConnectionState; } }
        public static Plugin UriCollector { get { return UriCollector; } set { if (value.Plug == typeof(UriCollectorPlugin)) UriCollector = value; } }
    }
}
